package scm;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
import java.io.*;
import java.net.*;
import java.sql.*;


public class Monitor extends JFrame
{
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JTextField jTextField1;
    private JTextField jTextField2;
    private JButton jButton1;
    private JPanel contentPane;
    private TcpClient client;

    public Monitor()
    {
        super();
	create();
        this.setVisible(true);
    }


    private void create()
    {
        jLabel1 = new JLabel();
	jLabel2 = new JLabel();
	jTextField1 = new JTextField();
	jTextField2 = new JTextField();
	jButton1 = new JButton();
	contentPane = (JPanel)this.getContentPane();

	jLabel1.setHorizontalAlignment(SwingConstants.LEFT);
	jLabel1.setForeground(new Color(0, 0, 255));
	jLabel1.setText("IP:");

	jLabel2.setHorizontalAlignment(SwingConstants.LEFT);
	jLabel2.setForeground(new Color(0, 0, 255));
	jLabel2.setText("Numer portu:");

	jTextField1.setForeground(new Color(0, 0, 255));
	jTextField1.setSelectedTextColor(new Color(0, 0, 255));
	jTextField1.setToolTipText("Podaj adres IP");

	jTextField2.setForeground(new Color(0, 0, 255));
	jTextField2.setToolTipText("Podaj numer portu");

	jButton1.setBackground(new Color(204, 204, 204));
	jButton1.setForeground(new Color(0, 0, 255));
	jButton1.setText("Połącz");
	jButton1.addActionListener(new ActionListener()
        {
	    public void actionPerformed(ActionEvent e)
	    {
		jButton1_actionPerformed(e);
	    }

	});

	contentPane.setLayout(null);
	contentPane.setBorder(BorderFactory.createEtchedBorder());
	contentPane.setBackground(new Color(204, 204, 204));
	addComponent(contentPane, jLabel1, 5,10,106,18);
	addComponent(contentPane, jLabel2, 5,47,97,18);
	addComponent(contentPane, jTextField1, 110,10,183,22);
	addComponent(contentPane, jTextField2, 110,45,183,22);
	addComponent(contentPane, jButton1, 150,75,83,28);

	this.setTitle("Łączenie z monitorem");
	this.setLocation(new Point(250,250));
	this.setSize(new Dimension(335, 141));
	this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	this.setResizable(false);
    }


    private void addComponent(Container container,Component c,int x,int y,int width,int height)
    {
	c.setBounds(x,y,width,height);
	container.add(c);
    }


    private void jButton1_actionPerformed(ActionEvent e)
    {
        String ip = new String(jTextField1.getText());
	Integer port = Integer.parseInt(jTextField2.getText());

        this.dispose();
        while (true)
        {
            client = new TcpClient(ip, port);
            client.start();
            String odczyt = client.read();
            System.out.println(odczyt);
            if (!odczyt.equals(""))
            {
                int liczba = Integer.parseInt(odczyt);
                intepretuj(liczba);

            }
            else if (odczyt.equals("end"))
            {
               break;
            }


        client.close();
        }
    }

    private void intepretuj(Integer liczba)
    {
        int procedureId = 0;
        if (liczba >= 85)
        {
            System.out.println(">=85");
            procedureId = 1;
        }
        else if (liczba <= 15)
        {
            System.out.println("<=15");
            procedureId = 2;
        }

        if (procedureId <= 0)
        {
            return;
        }

        try
        {
            Class.forName("org.postgresql.Driver");
        }
        catch (ClassNotFoundException cnfe)
        {
            JOptionPane.showMessageDialog(null, cnfe.getMessage());
        }
        try
        {
            c = DriverManager.getConnection("jdbc:postgresql://localhost/template1",
                                    "postgres", "przemek");

            PreparedStatement ps = c.prepareStatement("SELECT id_act FROM proc_act WHERE id_proc = ?");
            ps.setInt(1, procedureId);
            ResultSet rs = ps.executeQuery();

            Akcja action = new Akcja();

            while (rs.next())
            {
                Integer actionId = Integer.parseInt(rs.getString("id_act"));
                action.wykonaj(actionId);
                System.out.println("Wykonuje akcje " + actionId.toString() );
            }            
         }
         catch (SQLException se)
         {
             JOptionPane.showMessageDialog(null, se.getMessage());
         }
    }

    private Connection c = null;
}
